package com.tian.view.draw.paint

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.ComposePathEffect
import android.graphics.CornerPathEffect
import android.graphics.DashPathEffect
import android.graphics.DiscretePathEffect
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PathDashPathEffect
import android.graphics.SumPathEffect
import android.util.AttributeSet
import android.view.View
import com.tian.util.view.DisplayUtil

/**
 * @author tiansongtao
 * @date 2023/1/4 9:32
 *
 * 常用函数
 * [Paint.reset] 重置画笔
 * [Paint.setColor] 设置画笔颜色
 * [Paint.setAlpha]
 * [Paint.setStyle] 设置画笔样式
 * [Paint.setStrokeWidth] 画笔宽度
 * [Paint.setAntiAlias] 画笔抗锯齿
 * [Paint.setStrokeMiter] 画笔倾斜度，一般不用，看不出差别
 *
 * 不常用函数
 * [Paint.setPathEffect] 设置路径样式
 *
 * [Paint.setStrokeCap] 设置画笔头尾样式，称为线帽
 *   [Paint.Cap.BUTT]:无线帽 [Paint.Cap.ROUND]:圆线帽 [Paint.Cap.SQUARE]:方形线帽
 *
 * [Paint.setStrokeJoin] 设置画笔路径转角样式
 *  [Paint.Join.ROUND]:圆角转角 [Paint.Join.BEVEL]:横切面转角 [Paint.Join.MITER]:直角转角
 *
 * 字体函数
 * [Paint.setTextSize] 字体大小
 * [Paint.setFakeBoldText] 是否粗体
 * [Paint.setStrikeThruText] 带删除线
 * [Paint.setUnderlineText] 带下划线
 * [Paint.setTextAlign] 设置字体开始绘制相对于基线起始点位置
 * [Paint.setTextScaleX] 字体水平拉伸
 * [Paint.setTextSkewX] 字体倾斜度,普通斜体为 -0.25,向右倾斜
 * [Paint.setTypeface] 字体样式
 *
 * [Paint.setLinearText] 设置线性文本标识，省内存，但绘制速度减慢
 *
 */
class PaintView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
        View(context, attrs, defStyleAttr) {

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context) : this(context, null)

    val paint = Paint().apply {
        color = Color.RED
        strokeWidth = DisplayUtil.dpToPx(2f)
        style = Paint.Style.STROKE
        isAntiAlias = true
    }
    val textPaint = Paint().apply {
        color = Color.BLACK
        style = Paint.Style.FILL_AND_STROKE
        textSize = DisplayUtil.spToPx(14f)
    }

    val colors: IntArray = intArrayOf(Color.RED, Color.BLUE, Color.GREEN,
            Color.YELLOW, Color.BLACK, Color.MAGENTA, Color.CYAN)

    val p = Path().apply {
        addRect(0f, 0f, 8f, 8f, Path.Direction.CCW)
    }

    /**
     * 不常用，详解不再描述
     * CornerPathEffect 设置path拐角为圆角
     * DiscretePathEffect 设置path 分为线段并离散
     * DashPathEffect 设置path为虚线
     * PathDashPathEffect 使用path图形来绘制path
     *
     * ComposePathEffect 组合效果
     * SumPathEffect 叠加效果
     * */
    val effects = arrayOf(
            null, CornerPathEffect(20f), DiscretePathEffect(3f, 10f),
            DashPathEffect(floatArrayOf(20f, 10f, 10f, 20f), 0f),
            PathDashPathEffect(p, 12f, 0f, PathDashPathEffect.Style.ROTATE),
            ComposePathEffect(
                    DiscretePathEffect(3f, 10f),
                    PathDashPathEffect(p, 12f, 0f, PathDashPathEffect.Style
                        .ROTATE)),
            SumPathEffect(
                    DiscretePathEffect(3f, 10f),
                    PathDashPathEffect(p, 12f, 0f, PathDashPathEffect.Style
                        .ROTATE))
    )

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        var hStart = 60f
        canvas.drawText("Paint.setPathEffect", 50f, hStart, textPaint)
        val path = Path()
        path.moveTo(10f, 100f)
        path.lineTo(200f, 190f)
        path.lineTo(400f, 100f)
        path.lineTo(550f, 220f)
        hStart = 220f
        canvas.save()
        effects.forEachIndexed { index, effect ->
            paint.pathEffect = effect
            paint.color = colors[index]
            canvas.drawPath(path, paint)
            canvas.translate(0f, 60f)
            hStart += 60f
        }
        paint.pathEffect = null
        paint.color = Color.RED
        canvas.restore()


        canvas.drawText("Paint.setStrokeCap线帽", 50f, hStart, textPaint)
        hStart += 70f
        //画线的起始位置
        canvas.drawLine(100f, hStart - 40f, 100f, hStart + 200f, paint)
        paint.strokeWidth = DisplayUtil.dpToPx(15f)
        // 无线帽
        paint.strokeCap = Paint.Cap.BUTT
        canvas.drawLine(100f, hStart, 600f, hStart, paint)
        hStart += (paint.strokeWidth + 20f)
        // 圆形线帽，线段会多出来一部分
        paint.strokeCap = Paint.Cap.ROUND
        canvas.drawLine(100f, hStart, 600f, hStart, paint)
        hStart += (paint.strokeWidth + 20f)
        // 方形线帽，线段会多出来一部分
        paint.strokeCap = Paint.Cap.SQUARE
        canvas.drawLine(100f, hStart, 600f, hStart, paint)
        paint.strokeWidth = DisplayUtil.dpToPx(2f)
        paint.strokeCap = Paint.Cap.BUTT



        canvas.drawText("Paint.setStrokeJoin", 50f, hStart + 130f, textPaint)
        hStart += 130f
        path.reset()
        path.moveTo(50f, hStart + 40f)
        path.lineTo(150f, hStart + 40f)
        path.lineTo(150f, hStart + 140f)
        hStart += 180f
        paint.strokeWidth = DisplayUtil.dpToPx(10f)
        paint.strokeJoin = Paint.Join.ROUND
        canvas.drawPath(path, paint)
        path.offset(150f, 0f)
        paint.strokeJoin = Paint.Join.BEVEL
        canvas.drawPath(path, paint)
        path.offset(150f, 0f)
        paint.strokeJoin = Paint.Join.MITER
        canvas.drawPath(path, paint)


    }


}